package cn.chenxiejia;

import java.util.Arrays;

/***
 * @program: maven
 * @ClassName: String2
 * @Description:
 * @Author: chenxiejia
 * @Date: 2022/1/13 9:50
 * @version : V1.0
 */
public class Ali_task {
    public static void main(String[] args) {
        //1. 将一个字符串数组进行分组输出，每组中的字符串都由相同的字符组成。
        //举个例子：输入[“eat”,”tea”,”tan”,”ate”,”nat”,”bat”]
        //输出[[“ate”,”eat”,”tea”],[“nat”,”tan”],[“bat”]]


        String s1 = "abc,acb,cab,sfd,dsf,awe,ewa,sddd,erwe,wree";
        String[] s2 = s1.split(",");


        for (int i = 0; i < s2.length; i++) {
            String str = s2[i];
            for (int j = i + 1; j < s2.length; j++) {
                //为空的时候跳出本次循环
                if (s2[j].equals("")) {
                    continue;
                }
                //相同的字符串用——连接
                if (is(str, s2[j])) {
                    s2[i] += "_" + s2[j];
                    s2[j] = "";
                }
            }
        }
//        String ss = Arrays.toString(s2);
//        ss.replaceAll("\\n+", ",");
//        System.out.print("*".repeat(50));
//        System.out.println(ss);
//        StringBuilder su = new StringBuilder(Arrays.toString(s2));
//        for (int i = 0; i < s2.length - 1; i++) {
//            if (!",".equals(s2[i]) && ",".equals(s2[i + 1])) {
//                su.append(s2[i]);
//            }
//        }
        StringBuilder sb = new StringBuilder();
        String a = "";
        for (String s : s2) {
            if (!"".equals(s)) {

                sb.append("," + s);
            }
        }

        sb.delete(0, 1);
//        sb.append("]");
//        String c = "";
//        String[][] ee = new String[][]{{c}, {}};
        System.out.println("*".repeat(50));
        System.out.println(sb);
        String[] cc = sb.toString().split(",");

        String[][] v = new String[sb.length()][sb.toString().split("_&&,").length];
        one2Two(cc, v);
        System.out.println(Arrays.deepToString(v));


//        System.out.println(su);
//        System.out.println();
//        System.out.println(s2[0]);
//        StringBuilder sub = new StringBuilder("[");
//        String s3 = Arrays.toString(s2);
//        System.out.println(s3);
//if (new String(s2).split("_")||)
    }

    public static boolean is(String a, String b) {
        boolean f = false;
        if (a.length() == b.length()) {
            var s1 = a.toCharArray();
            Arrays.sort(s1);
            var s2 = b.toCharArray();
            Arrays.sort(s2);
            if (new String(s1).equals(new String(s2))) {
                f = true;
            }

        }
        return f;
    }

    public static void one2Two(String[] data, String[][] da) {
        int k = 0;
        int hang = da.length;

        for (int i = 0; i < hang; i++) {
            for (int j = 0; j < data[i].split("_").length; j++) {
                da[i][j] = data[i];
                k++;
            }
        }
    }
}

